package h0910;

import java.util.*;

/**
  * @description 最常见的单词
  * @author 不知名帅哥
  * @date 2024/9/10 23:12
  * @version 1.0
*/
public class MostCommonWord {
    public static void main(String[] args) {

    }
    public String mostCommonWord(String paragraph, String[] banned) {
        String s = paragraph.toLowerCase();
        String s1 = s.replaceAll("[^a-zA-Z]", " ");
        String[] words = s1.split("\\s+");
        HashSet<String> banWords = new HashSet<>(Arrays.asList(banned));
        HashMap<String, Integer> map = new HashMap<>();
        int maxCount=0;
        String res="";
        for (String word : words) {
            if (!banWords.contains(word)){
                int count = map.getOrDefault(word, 0) + 1;
                map.put(word, count);
                if (count>maxCount){
                    maxCount=count;
                    res=word;
                }
            }
        }
       return res;
    }
}
